﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ASS.BUS.DTO;
using ASS.BUS.Services;

namespace ASS
{
    public partial class FrmEditLichHen : Form
    {
        private FrmMainClinicStaff _frmMain;
        private LichHen _lichHen;
        private BenhNhan _benhNhan = new BenhNhan();
        private BenhNhanServices _benhNhanServices = new BenhNhanServices();
        private NhanVien _nhanVien = new NhanVien();
        private NhanVienServices _nhanVienServices = new NhanVienServices();
        public FrmEditLichHen()
        {
            InitializeComponent();
        }

        public FrmEditLichHen(LichHen lichHen, FrmMainClinicStaff frmMain)
        {           
            InitializeComponent();
            _lichHen = lichHen;
            _frmMain = frmMain;
        }

        private void FrmEditLichHen_Load(object sender, EventArgs e)
        {
            var nhanViens = _nhanVienServices.GetBacSis();
            var nhanVienList = nhanViens.Select(nv => nv.MaNv).ToList();
            cboDoctor.DataSource = nhanVienList;

            txtID.Text = _lichHen.MaLh.ToString();
            txtID.Enabled = false;
            dtpDateAppointment.Text = _lichHen.NgayHen;
            rtfContent.Text = _lichHen.NoiDung;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            LichHenServices lichHenServices = new LichHenServices();

            if (_benhNhan != null)
            {
                _lichHen.NgayHen = dtpDateAppointment.Value.ToString();
                _lichHen.NoiDung = rtfContent.Text;
                if (_nhanVien != null)
                {
                    _lichHen.NhanVien = _nhanVien;
                    _lichHen.BacSi = _nhanVien.Ten;
                }

                bool editSuccess = lichHenServices.EditLichHen(_lichHen);
                MessageBox.Show((editSuccess /*&& addThanNhan*/) ? "Đã chỉnh sửa thông tin lịch hẹn!" : "Không sửa thông tin lịch hẹn!", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                _frmMain.dvgLichhen.DataSource = lichHenServices.GetLichHens();
                this.Close();
            }
        }

        private void cboDoctor_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboDoctor.SelectedIndex == -1) return;
            var id = Int32.Parse(cboDoctor.SelectedValue.ToString());
            _nhanVien = _nhanVienServices.GetNhanVien(id);
        }

    }
}
